var DateUtils = DateUtils || {};
DateUtils.DateConf = {
    DAY: 24 * 3600 * 1000, //天
    HOUR: 3600 * 1000, //小时
    MINUTE: 60 * 1000, //分
    SECOND: 1000 //秒
}

// 转换时间戳的差值 为 x天X时x分x秒
DateUtils.transTime = function (time) {
    if (time == null) {
        return null;
    }
    var d = parseInt(time / this.DateConf.DAY),
        h = parseInt((time % this.DateConf.DAY) / this.DateConf.HOUR),
        m = parseInt((time % this.DateConf.HOUR) / this.DateConf.MINUTE),
        s = parseInt((time % this.DateConf.MINUTE) / this.DateConf.SECOND);
    return (d > 0 ? '' + d + '天' : '')
        + (h > 0 ? '' + h + '小时' : '')
        + (m > 0 ? '' + m + '分' : '')
        + (s >= 0 ? '' + s + "秒" : '');
}

Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

/**
 * 格式化金钱格式为 1,000,000.00
 * @param num 钱
 */
function fmtMoney(num) {
    if (checkIsEmpty(num)) {
        return 0;
    }
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
            num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
}


/**
 * 获取选中id
 * @returns {Array}
 */
function getTableSelect(obj) {
    var datas = $(obj).dataTableExtend('getCheck');
    var ids = [];
    for (var i = 0; i < datas.length; i++) {
        if (datas[i].id)
            ids.push(datas[i].id);
    }
    return ids;
}

/**
 * 表格多选框验证
 * @param ids
 * @param isSingle 是否只能单选 0-是 1-否 默认不判断单选
 */
function checkTableSelect(ids, isSingle) {
    if (ids.length <= 0) {
        DGG.Errors('请先勾选数据');
        return false;
    } else {
        if (!checkIsEmpty(isSingle) && isSingle == 0 && ids.length > 1) {
            DGG.Errors('只能选择一条数据');
            return false;
        }
        return true;
    }

}

/**
 * 获取select2多选的值
 * @param obj
 * @param toStr 是否拼接字符串 0-是  1-否 默认不拼接
 * @returns
 */
function getVal(obj, toStr) {
    var strtext = $(obj).select2("val");
    if (checkIsEmpty(toStr) || toStr === 1) {
        return strtext;
    } else {
        var str = "";
        if (!checkIsEmpty(strtext) && strtext.length > 0) {
            for (var i = 0; i < strtext.length; i++) {
                str += strtext[i] + ",";
            }
            return str.substring(0, str.length - 1);
        } else {
            return;
        }
    }
}


/**
 * 获取多选下拉框的文本框值
 */
function getTextVal(obj) {
    var strtext = $(obj).select2("data");
    var str = "";
    if (!checkIsEmpty(strtext) && strtext.length > 0) {
        for (var i = 0; i < strtext.length; i++) {
            str += strtext[i].text + ",";
        }
        return str.substring(0, str.length - 1);
    } else {
        return;
    }

}

/**
 * 将表单元素序列化成一个属性对象
 * @param options 可用序列化参数：
 * {
     * allowEmpty:false,
     * includeHidden:true,
     * trimString:true
     * }
 * @return object
 */
$.fn.serialize2object = function (options) {
    var setting = {};
    setting.allowEmpty = false;//序列化空值
    setting.includeHidden = true;//序列化隐藏值
    setting.trimString = true;//trim值
    setting.extra = undefined;//额外序列化标签
    setting.base = "input,select,textarea";//序列化标签
    $.extend(setting, options);
    var data = {};
    var tags = setting.base;
    if (setting.extra != undefined) {
        tags = tags + "," + setting.extra;
    }
    var items = $(this).find(tags);
    if (items.size() == 0) {
        console.log("no filed found in this container");
        return data;
    }
    items.each(function (i, e) {
        var item = $(e);
        var hidden = item.css("display") == "none";
        var name = item.attr("name");
        //没有name属性不序列化
        if (name == undefined || name == "") return;
        var baseTags = setting.base.split(",");
        var isBase = false;
        for (var i = 0; i < baseTags.length; i++) {
            if (item.is(baseTags[i])) {
                isBase = true;
                break;
            }
        }
        var value;
        if (isBase) {
            value = item.val();
        }
        else {
            value = item.text();
        }
        //配置要求隐藏元素不序列化
        if (hidden && !setting.includeHidden) return;
        //控件值异常不序列化
        if (value == undefined) return;
        value = value.trim();
        //配置要求空值不序列化
        if (value == "" && setting.allowEmpty == false) return;
        data[name] = value;
    });
    for (var key in data) {
        var value = data[key];
        //去掉字符串前后的空格
        if (setting.trimString) {
            value = typeof(value) == "string" ? value.trim() : value;
            data[key] = value;
        }
        value = data[key];
        //是否允许空值
        if (!setting.allowEmpty && value == "") {
            delete data[key];
        }
    }
    return data;
}


/**
 * 隐藏指定位数字符串
 * @param str
 * @param frontLen 开始
 * @param endLen 结束
 * @returns {string}
 */
function hideStr(str, frontLen, endLen) {
    var len = str.length - frontLen - endLen;
    var xing = '';
    for (var i = 0; i < len; i++) {
        xing += '*';
    }
    return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
}
